import { reqConditionList,reqAddRole,reqGetSelectedRoles,reqGetBannerList,
    reqGetAllRoles,reqDeleteARoleById,reqSearchByName,reqUpdateRoleInfo,reqFindRolesForPagination} from "@/api"

const actions = {
    //获取筛选条件
    async getConditionList({commit}){
        let result = await reqConditionList()
        if(result.code == 200){
            commit('GETCONDITIONLIST',result.data)
        }
    },

    //新建角色
    async createRole({commit},roleInfo){
        let result = await reqAddRole(roleInfo)
        if(result.code == 200){
            console.log(result.data);
            return '创建成功！'
        }else{
            return Promise.reject('创建失败')
        }
    },

    //获取筛选条件下的角色列表  
    async getSelectedRoles({commit},conditions){
        let result = await reqGetSelectedRoles(conditions)

        if(result.code == 200){
            commit("GETSELECTEDROLES",result.data)
        }
    },

    //获取首页轮播图
    async getBannerList({commit}){
        let result = await reqGetBannerList()

        if(result.code == 200){
            commit("GETBANNERLIST",result.data)
        }
    },

    //获取所有角色信息
    async getAllRoles({commit}){
        let result = await reqGetAllRoles()
        if(result.code == 200){
            commit("GETALLROLES",result.data)
        }
    },

    //删除一个角色
    async deleteARoleById({commit},id){
        let result = await reqDeleteARoleById(id)
        if(result.code == 200){
            return result.msg
        }else{
            return Promise.reject(result.msg)
        }
    },

    //根据name查询指定角色
    async getSearchResult({commit},name){
        let result = await reqSearchByName(name)

        if(result.code == 200){
            commit("GETSEARCHRESULT",result.data)
        }
    },

    //根据角色_id，修改角色信息
    async updateRoleInfo(context,value){
        let _id = value._id
        delete value._id
        let result = await reqUpdateRoleInfo(_id,JSON.stringify(value))

        return result.msg    
    },

    //获取pagination条件下的角色信息
    async getRoleInfoForPagination({commit},value){
        let result = await reqFindRolesForPagination(value.pageNo,value.pageSize)

        if(result.code == 200){
            commit("GETROLEINFOFORPAGINATION",result)
        }
    }
}

const mutations = {
    GETCONDITIONLIST(state,conditionList){
        state.conditionList = conditionList || []
    },

    GETSELECTEDROLES(state,selectedRoleList){
        state.selectedRoleList = selectedRoleList || []
    },

    GETBANNERLIST(state,bannerList){
        state.bannerList = bannerList || []
    },

    GETALLROLES(state,allRoleList){
        state.allRoleList = allRoleList
    },

    GETSEARCHRESULT(state,searchResult){
        state.searchResult = searchResult
    },

    GETROLEINFOFORPAGINATION(state,value){
        state.total = value.total
        state.roleInfoArr = value.data
    }
}

const state = {
    conditionList:[],
    selectedRoleList:[],
    bannerList:[],
    allRoleList:[],
    searchResult:[],
    roleInfoArr:[],
    total:0
}

const getters = {

}

export default {
    actions,
    mutations,
    state,
    getters
}